/**
 * @author planderos
 */

var cursorPosition = 1;

function popupButtonHandler(e)
{
    switch(e.buttonId)
    {
        case dcKeyEvent.BUTTON_ID_BACK:
        {
			var dctk = connected.toolkit;
			dctk.dismissPopup();
        }
        break;

    }
}

function itemSelected()
{
	var dctk = connected.toolkit;
	cursorPosition = itemList.curSel;
	if (cursorPosition == 0) {
		connected.debug.print("AU");
		reg = "AU";
		dctk.dismissPopup();
		onLoadComplete();
	}
	else if (cursorPosition == 1) {
		connected.debug.print("BR");
		reg = "BR";
		dctk.dismissPopup();
		onLoadComplete();
	}
	else if (cursorPosition == 2) {
		connected.debug.print("CA");
		reg = "CA";
		dctk.dismissPopup();
		onLoadComplete();
	}
	else if (cursorPosition == 3) {
		connected.debug.print("FR");
		reg = "FR";
		dctk.dismissPopup();
		onLoadComplete();
	}
	else if (cursorPosition == 4) {
		connected.debug.print("DE");
		reg = "DE";
		dctk.dismissPopup();
		onLoadComplete();
	}
	else if (cursorPosition == 5) {
		connected.debug.print("GB");
		reg = "GB";
		dctk.dismissPopup();
		onLoadComplete();
	}
	else if (cursorPosition == 6) {
		connected.debug.print("NL");
		reg = "NL";
		dctk.dismissPopup();
		onLoadComplete();
	}
	else if (cursorPosition == 7) {
		connected.debug.print("HK");
		reg = "HK";
		dctk.dismissPopup();
		onLoadComplete();
	}
	else if (cursorPosition == 8) {
		connected.debug.print("IE");
		reg = "IE";
		dctk.dismissPopup();
		onLoadComplete();
	}
	else if (cursorPosition == 9) {
		connected.debug.print("IT");
		reg = "IT";
		dctk.dismissPopup();
		onLoadComplete();
	}
	else if (cursorPosition == 10) {
		connected.debug.print("JP");
		reg = "JP";
		dctk.dismissPopup();
		onLoadComplete();
	}
	else if (cursorPosition == 11) {
		connected.debug.print("MX");
		reg = "MX";
		dctk.dismissPopup();
		onLoadComplete();
	}
	else if (cursorPosition == 12) {
		connected.debug.print("NZ");
		reg = "NZ";
		dctk.dismissPopup();
		onLoadComplete();
	}
	else if (cursorPosition == 13) {
		connected.debug.print("PL");
		reg = "PL";
		dctk.dismissPopup();
		onLoadComplete();
	}
	else if (cursorPosition == 14) {
		connected.debug.print("RU");
		reg = "RU";
		dctk.dismissPopup();
		onLoadComplete();
	}
	else if (cursorPosition == 15) {
		connected.debug.print("KR");
		reg = "KR";
		dctk.dismissPopup();
		onLoadComplete();
	}
	else if (cursorPosition == 16) {
		connected.debug.print("ES");
		reg = "ES";
		dctk.dismissPopup();
		onLoadComplete();
	}
	else if (cursorPosition == 17) {
		connected.debug.print("TW");
		reg = "TW";
		dctk.dismissPopup();
		onLoadComplete();
	}
	else if (cursorPosition == 18) {
		connected.debug.print("US");
		reg = "US";
		dctk.dismissPopup();
		onLoadComplete();
	}
	
}

function createItemList(){
	var item;
	var dctk = connected.toolkit;
		item = itemList.addItem("AU");
    	item.text = "Australia";
		item = itemList.addItem("BR");
    	item.text = "Brazil";
		item = itemList.addItem("CA");
    	item.text = "Canada";
		item = itemList.addItem("FR");
    	item.text = "France";
		item = itemList.addItem("DE");
    	item.text = "Germany";
		item = itemList.addItem("GB");
    	item.text = "Great Britain";
		item = itemList.addItem("NL");
    	item.text = "Holland";
		item = itemList.addItem("HK");
    	item.text = "Hong Kong";
		item = itemList.addItem("IE");
    	item.text = "Ireland";
		item = itemList.addItem("IT");
    	item.text = "Italy";
		item = itemList.addItem("JP");
    	item.text = "Japan";
		item = itemList.addItem("MX");
    	item.text = "Mexico";
		item = itemList.addItem("NZ");
    	item.text = "New Zeland";
		item = itemList.addItem("PL");
    	item.text = "Poland";
		item = itemList.addItem("RU");
    	item.text = "Russia";
		item = itemList.addItem("KR");
    	item.text = "South Korea";
		item = itemList.addItem("ES");
    	item.text = "Spain";
		item = itemList.addItem("TW");
    	item.text = "Taiwan";
		item = itemList.addItem("US");
    	item.text = "United States";
}

function showPopup()
{
    var dctk = connected.toolkit;
    
    // create root panel
    var popup = dctk.createPanelCtrl("popup", true);
    popup.width = 50;
    popup.height = 80;
    popup.outlined = true;
    popup.filled = true;
    popup.color.rgb = 0x000000;
    popup.color.a = 192;
    popup.onUnhandledKeyEvent = popupButtonHandler;
	

/*
	var popupTitle = dctk.createLabelCtrl("popupTitle", true);
    popupTitle.font = dctk.createFontByFacename("DIN-Bold", false, false);
    popupTitle.setRect(10, 280, 230, 310);
    popupTitle.textColor.rgb = 0xcccccc;
    popupTitle.fontSize = 32;
    popupTitle.align = dctkLabel.TS_CENTER;
	popupTitle.valign = dctkLabel.TS_MIDDLE;
    popupTitle.text = "SHOUTcast - Genres";
    popup.addChild(popupTitle);
*/

	itemList = dctk.createListCtrl("itemList", true);
    itemList.setRect(0, 0, 50, 80);
	itemList.cursorImg.src = "chrome://connected/content/dark_gray_cursor_hi.png";
    itemList.cursorImg.async = false;
    itemList.maxDisplayItems = 10;
	itemList.marquee = true;
	itemList.showPageIndicator = false;
	itemList.textColor.rgb = 0xFFFFFF;
	itemList.onSelect = itemSelected;
	popup.addChild(itemList);
	
	createItemList();
	
	// show the popup
    dctk.invokePopup(popup);
	
	//connected.toolkit.debug.drawElementOutline = true;

}